//
//  FileHelper.m
//  AutoCompilerManager
//
//  Created by Vescky on 13-7-11.
//  Copyright (c) 2013年 Vescky. All rights reserved.
//

#import "FileHelper.h"

@implementation FileHelper

FileHelper *fileHelper;

+ (id)shareFileHelper {
    if (!fileHelper) {
        fileHelper = [[self alloc] init];
    }
    return fileHelper;
}

//清空一个目录
- (bool)cleanFolder:(NSString*)path {
    NSFileManager *fm = [NSFileManager defaultManager];
    if (![fm fileExistsAtPath:path]) {
        [fm createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];
    }
    
    NSMutableArray *arr = [self getNameListOfFolder:path];
    if (!arr || [arr count] <= 0) {
        return NO;
    }
    
    for (int i = 0; i < [arr count]; i++) {
        NSString *fName = [arr objectAtIndex:i];
        [fm removeItemAtPath:[path stringByAppendingPathComponent:fName] error:nil];
    }
    return YES;
}

//获取该层目录所有项目的name
- (NSMutableArray*)getNameListOfFolder:(NSString*)folderName {
    NSFileManager *fm = [NSFileManager defaultManager];
    NSMutableArray *fileList = [[NSMutableArray alloc] initWithArray:[fm contentsOfDirectoryAtPath:folderName error:nil]];
    return fileList;
}

//复制目录里面的内容到另一个目录中
- (bool)copyItemsInFolder:(NSString*)oPath ToFolder:(NSString*)gPath {
    NSFileManager *fm = [NSFileManager defaultManager];
    BOOL isDir,isDirG;
    if (![fm fileExistsAtPath:oPath isDirectory:&isDir] || ![fm fileExistsAtPath:gPath isDirectory:&isDirG]) {
        return NO;
    }
    if (!isDir || !isDirG) {
        return NO;
    }
    
    NSArray *arr = [fm contentsOfDirectoryAtPath:oPath error:nil];
    if ([arr count] < 1) {
        return NO;
    }
    
    for (int i = 0; i < [arr count]; i++) {
        [fm copyItemAtPath:[oPath stringByAppendingPathComponent:[arr objectAtIndex:i]] toPath:[gPath stringByAppendingPathComponent:[arr objectAtIndex:i]]  error:nil];
    }
    return YES;
}


@end
